//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Spam;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServerWeb.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for SpamRuleSettings.
	/// </summary>
	public class SpamRuleSettings : BaseToolsPage
	{
		#region Members
		protected Repeater SettingsList;
		protected IButton SaveButton;
		#endregion

		protected override void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.SettingsList.ItemDataBound +=new RepeaterItemEventHandler(this.SettingsList_ItemDataBound);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
				this.DataBind();
		}

		public override void DataBind()
		{
			Guid RuleID = CSContext.Current.GetGuidFromQueryString("ruleid");

			if (RuleID != Guid.Empty)
			{
				// Get all settings
				SpamRule rule = SpamRules.GetRule(RuleID);
				ArrayList settings = rule.GetAvailableSettings();

				// Set values from config
				SpamConfiguration config = SpamConfiguration.Instance();
				Hashtable configSettings = rule.GetConfiguredSettings();
				foreach (RuleSetting setting in settings)
				{
					if (configSettings.ContainsKey(setting.Key))
						setting.Value = (configSettings[setting.Key] as RuleSetting).Value;
				}

				SettingsList.DataSource = settings;
				SettingsList.DataBind();
			}
		}

		private void SaveSettings()
		{
			Guid RuleID = CSContext.Current.GetGuidFromQueryString("ruleid");
			if (RuleID != Guid.Empty)
			{
				// Get all settings
				SpamRule rule = SpamRules.GetRule(RuleID);
				ArrayList settings = rule.GetAvailableSettings();

				// Update the values from the textboxes
				for (int i = 0; i < SettingsList.Items.Count; i++)
				{
					Literal key = SettingsList.Items[i].FindControl("Key") as Literal;
					if(key != null)
					{
						foreach (RuleSetting setting in settings)
						{
							if (setting.Key == key.Text)
							{
								setting.Value = ((TextBox)SettingsList.Items[i].FindControl("ConfigValue")).Text;
								break;
							}
						}
					}
				}

				// Save the rule settings
				SpamConfiguration config = SpamConfiguration.Instance(false);
				SpamRules.MergeSettings(config, settings);
				config.Save();
			}
		}

		#region Events

		private void SaveButton_Click(Object sender, EventArgs e)
		{
			if (!Page.IsValid)
				return;

			SaveSettings();

			Modal.ClosePage(this.Page);
		}


		private void SettingsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
			{
				RuleSetting setting = e.Item.DataItem as RuleSetting;
				if (setting != null)
				{
					Literal Key = e.Item.FindControl("Key") as Literal;
					if (Key != null)
						Key.Text = setting.Key;

					Literal Desc = e.Item.FindControl("Desc") as Literal;
					if (Desc != null)
						Desc.Text = setting.Description;

					TextBox ConfigValue = e.Item.FindControl("ConfigValue") as TextBox;
					if (ConfigValue != null)
					{
						ConfigValue.Text = setting.Value;
						ConfigValue.MaxLength = setting.MaxValueLength;
						if (setting.MaxValueLength > 75)
						{
							ConfigValue.TextMode = TextBoxMode.MultiLine;
							ConfigValue.Rows = 6;
							ConfigValue.Columns = 75;
						}
						else
						{
							ConfigValue.TextMode = TextBoxMode.SingleLine;
							ConfigValue.Columns = setting.MaxValueLength;
						}
					}
				}
			}
		}

		#endregion
	}
}
